package com.whu.zj;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.LruCache;
import android.widget.ImageView;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * 网络请求单例
 * Created by 凯 on 2015/10/16.
 */
public class VolleySingleton {
    private static VolleySingleton volleySingleton;
    private RequestQueue mRequestQueue;
    private ImageLoader mImageLoader;
    private Context mContext;

    public VolleySingleton(Context context) {
        this.mContext = context;
        mRequestQueue = getRequestQueue();
        mImageLoader = new ImageLoader(mRequestQueue,
                new ImageLoader.ImageCache() {
                    private final LruCache<String, Bitmap> cache = new LruCache<String, Bitmap>(20);

                    @Override
                    public Bitmap getBitmap(String url) {
                        return cache.get(url);
                    }

                    @Override
                    public void putBitmap(String url, Bitmap bitmap) {
                        cache.put(url, bitmap);
                    }
                });
    }

    public static synchronized VolleySingleton getVolleySingleton(Context context) {
        if (volleySingleton == null) {
            volleySingleton = new VolleySingleton(context);
        }
        return volleySingleton;
    }

    public static synchronized VolleySingleton getVolleySingleton() {
        if (volleySingleton == null) {
            volleySingleton = new VolleySingleton(MyApplication.share());
        }
        return volleySingleton;
    }

    public RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            mRequestQueue = Volley.newRequestQueue(mContext.getApplicationContext());
        }
        return mRequestQueue;
    }

    public <T> void addToRequestQueue(Request<T> req) {
        req.setRetryPolicy(new DefaultRetryPolicy(20*1000,1,1.0f));
        getRequestQueue().add(req);
    }

    public ImageLoader getImageLoader() {
        return mImageLoader;
    }

    public void displayImg(ImageView imageView, String uri) {
        ImageLoader.ImageListener listener = ImageLoader.getImageListener(imageView, R.drawable.default_picture, R.drawable.default_picture);
        mImageLoader.get(uri, listener, 500, 500);

    }

    // 对汉语进行utf-8编码
    public static String encodeUrl(String str) {
        StringBuffer sb = new StringBuffer();
        try {
            String[] urls = str.split("/");
            for (String str2 : urls) {
                if (str2 != null && !str2.isEmpty()) {
                    String temp = null;
                    temp = URLEncoder.encode(str2, "utf-8");
                    sb.append(temp).append("/");
                }

            }
            sb.deleteCharAt(sb.length() - 1);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return sb.toString();
    }
}